/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.boundary;

import com.sunrise.entity.Event;
import com.sunrise.entity.Location;
import com.sunrise.entity.User;
import com.sunrise.entity.WeatherAlert;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Andrea
 */

@Stateless
public class EventManager {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @EJB
    private LocationManager locationManager;
    
    @EJB
    private WeatherAlertManager weatherAlertManager;
    
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public void save(Event event) {
        event.sanitizeLocation();
        
        if(event.getLocationStandard() != null) {
            if (event.isOutdoor()) {
                List<Location> locations = locationManager.findByPlaceIdentifier(event.getLocationStandard().getPlaceIdentifier());
                if(locations.size() > 0) {
                    if(locations.get(0) != null) event.setLocation(locations.get(0));
                }
            } else {
                Location tmpLocation = new Location(event.getLocationStandard().getDescription(), "", "", "");
                    event.setLocation(tmpLocation);
            }
        }
        
        entityManager.persist(event);
    }
    
    public Event update(Event event) {
        event.sanitizeLocation();
        
        if (event.getLocationStandard() != null) {
            if (event.isOutdoor()) {
                List<Location> locations = locationManager.findByPlaceIdentifier(event.getLocationStandard().getPlaceIdentifier());
                if(locations.size() > 0) {
                    if(locations.get(0) != null) event.setLocation(locations.get(0));
                } else {
                    Location tmpLocation = new Location(event.getLocationStandard().getDescription(), event.getLocationStandard().getPlaceIdentifier(),
                            event.getLocationStandard().getLatitude(), event.getLocationStandard().getLongitude());
                    event.setLocation(tmpLocation);
                }
            } else {
                Location tmpLocation = new Location(event.getLocationStandard().getDescription(), "", "", "");
                    event.setLocation(tmpLocation);
            }
        }
        
        return entityManager.merge(event);
    }
    
    public List<Event> findAllEvents() {
        List<Event> result = new ArrayList<Event>();
        
        try {
            result = entityManager.createNamedQuery(Event.findAll, Event.class)
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result; 
    }
    
    public List<Event> findOutdoorEvents() {
        List<Event> result = new ArrayList<Event>();
        
        try {
            result = entityManager.createNamedQuery(Event.findOutdoorEvents, Event.class)
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result; 
    }
    
    public List<Event> findUserEvents() {
        List<Event> result = new ArrayList<Event>();
        
        try {
            result = entityManager.createNamedQuery(Event.findByUser, Event.class)
                        .setParameter("owner", getUser())
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result; 
    }
    
    public Event findEventByIdentifier(Long identifier) {
        Event result = null;
        
        try {
            result = entityManager.createNamedQuery(Event.findByIdentifier, Event.class)
                        .setParameter("identifier", identifier)
                        .getSingleResult();
        } catch (Exception ex) {

        }
        
        return result;
    }
    
    public List<Event> findUserInvitedAndAcceptedEvents() {
        List<Event> result = new ArrayList<Event>();
        
        try {
            result = entityManager.createNamedQuery(Event.findByUserInvitationAccepted, Event.class)
                        .setParameter("user", getUser())
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result; 
    }
    
    /*
    public List<Location> findByPlaceIdentifier(String placeIdentifier) {
        List<Location> result = new ArrayList<Location>();
        
        try {
            result = entityManager.createNamedQuery(Location.findByPlaceIdentifier, Location.class)
                        .setParameter("placeIdentifier", placeIdentifier)
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result; 
    }
    */
    
    public boolean deleteEvent(Event event) {
        try {
            List<WeatherAlert> weatherAlerts = weatherAlertManager.findWeatherAlertsForEvent(event);
            for(WeatherAlert weatherAlert : weatherAlerts) {
                weatherAlertManager.delete(weatherAlert);
            }
            
            Event persistedEntity = entityManager.getReference(Event.class, event.getIdentifier());
            entityManager.remove(persistedEntity);
        } catch (TransactionRequiredException | IllegalArgumentException ex) {
            return false;
        }
        
        return true;
    }
}
